import { Ajax } from '@/api/common'
import sessionStorage from '@/api/Storage'
import 'url-search-params-polyfill'
export function Login(self, _url, _parm) {
  Ajax(
    self,
    _url,
    _parm,
    'post',
    function(data) {
      // self.$message({ message:data, type: "success" });
      var body = data.data

      if (!body.success) {
        // alert(body.message)
        self.$message.error(body.message)
        self.loading = false
      } else {
        self.$message({ message: '登录成功', type: 'success' })

        // 写入本地信息
        sessionStorage.setItem({ name: 'id', value: body.data.data.id })
        sessionStorage.setItem({ name: 'token', value: body.data.data.jwt })
        sessionStorage.setItem({ name: 'userinfo', value: JSON.stringify(body.data.data) })
        sessionStorage.setItem({ name: 'userActionList', value: JSON.stringify(body.data.data.userActionList) })
        var loginrotes = ['/login/index',  '/404',  '/401']
        // self.$router.push({ path: self.redirect || '/' })
        var mens = body.data.menu
        self.$router.options.routes = []

        var i = 0
        // 首页管理者看板
        /**
       *     self.$router.options.routes.push({
          path: '/',
          component: self.Layout,
          redirect: '/manageboard',
          children: [
            {
              path: '/manageboard',
              name: 'index',
              component: () => import('@/views/manageboard/index'),
              meta: { title: '支付驾驶舱', icon: 'dashboard', affix: true }
            }
          ]
        })
       *
       */
        // console.log(mens);
        mens.forEach(element => {
          // 动态添加路由
          if (element.menus.length > 0) {
            var childrens = []
            // eslint-disable-next-line no-undef
            var pid = ''
            element.menus.forEach(element => {
              if (element.menus.length > 0 && element.menus.filter(x => x.menuname == '浏览').length > 0) {
                var url = element.menus.filter(x => x.menuname == '浏览')[0].url
                var urls = url.split('/')
                pid = urls[2]
                childrens.push({
                  path: url,
                  name: pid,
                  component: () => import('@/views' + url),
                  meta: { title: element.menuname, icon: '', keepAlive: true },
                  params: { 'menuid': element.menuid }
                })
                loginrotes.push(url)
              }
            })
            // 由下级菜单才显示
            if (childrens.length > 0) {
              self.$router.options.routes.push({
                path: '/' + pid,
                meta: { title: element.menuname, icon: element.modulecls, keepAlive: true },
                children: childrens,
                params: { 'menuid': element.menuid }
              })
            }
          } else {
            //取消单页生产
          }
          i++
        })
        localStorage.setItem('Role', JSON.stringify(loginrotes))
        sessionStorage.setItem({
          name: 'route',
          value: JSON.stringify(self.$router.options.routes)
        }
        )
        // debugger
        self.$router.push({ path: self.$router.options.routes[0].children[0].path })
        sessionStorage.setItem({ name: 'loginrotes', value: loginrotes })

        self.loading = false
      }
    },
    function(error) {
      self.loading = false
      self.$message.error('接口请求失败')
    }
  )
}
